from datetime import datetime

from django.db import models

# Create your models here.
from DjangoUeditor.models import UEditorField

from apps.organization.models import Teacher, CourseOrg
from apps.users.models import BaseModel

degree_chioes = (('cj', '初级'), ('zj', '中级'), ('gj', '高级'))


# 课程信息
class Courses(BaseModel):
    # 课程属于什么讲师讲课
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="讲师")  # 级联删除
    # 冗余存贮数据方便反向查询出课程机构的课
    course_org = models.ForeignKey(CourseOrg, null=True, blank=True, on_delete=models.CASCADE, verbose_name="课程机构")
    name = models.CharField(verbose_name="课程名", max_length=50)
    desc = models.CharField(verbose_name="课程描述", max_length=300)
    degree = models.CharField(verbose_name="难度", choices=degree_chioes, max_length=2)
    students = models.IntegerField(verbose_name="学习人数", default=0)
    fav_nums = models.IntegerField(verbose_name="收藏人数", default=0)
    click_nums = models.IntegerField(verbose_name="点击数", default=0)
    category = models.CharField(verbose_name="课程类型", max_length=20, default=u"后端开发")
    tag = models.CharField(verbose_name="课程标签", max_length=10)
    youneed_know = models.CharField(verbose_name="课程须知", max_length=300, default="")
    teacher_tell = models.CharField(verbose_name="老师告诉你", max_length=300, default="")
    # detail = models.TextField(verbose_name="课程详情")
    detail = UEditorField(verbose_name="课程详情", width=950, height=500, imagePath="courses/ueditor/images/",
                          filePath="courses/ueditor/files/", default="")
    image = models.ImageField(verbose_name="封面图", upload_to="courses/%Y/%m")
    learn_times = models.IntegerField(default=0, verbose_name=u"学习时长(分钟数)")
    is_classics = models.BooleanField(default=False, verbose_name="是否经典")
    notice = models.CharField(verbose_name="课程公告", max_length=300, default="")
    is_banner = models.BooleanField(default=False, verbose_name="是否广告位")

    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def lesson_all(self):
        return self.lesson_set.all()

    def show_image(self):
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='{}' style='width: 160px;height: 100px'>".format(self.image.url))

    show_image.short_description = "封面"

    def go_to(self):
        from django.utils.safestring import mark_safe
        return mark_safe("<a href='/course/detail/{}'>跳转</a>".format(self.id))

    go_to.short_description = "详情页"

    def lesson_nums(self):
        return self.lesson_set.all().count()

    def study_student_course(self):
        return self.usercourse_set.all()[:5]

    # def study_student(self):
    #     return self.usercourse_set.
    # def get_video_id(self):
    # return self.lesson_set.video_all
    def discuss_all(self):
        return self.coursediscuss_set.all().order_by('-add_time')

    def home_work_all(self):
        return self.homework_set.all()


# 课程章节     课程信息 1对多课程章节
class Lesson(BaseModel):
    course = models.ForeignKey(Courses, on_delete=models.CASCADE, verbose_name="课程")  # 级联删除
    name = models.CharField(verbose_name=u"章节名", max_length=100)
    learn_times = models.IntegerField(verbose_name="学习时长", default=0)

    class Meta:
        verbose_name = "课程章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def video_all(self):
        return self.video_set.all()


# 视频视频 课程信息 1对多 章节视频
class Video(BaseModel):
    lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.CASCADE)
    name = models.CharField(verbose_name="视频名", max_length=100)
    learn_times = models.IntegerField(default=0, verbose_name="学习时长（分钟数）")
    video_file = models.FileField(upload_to="course/videos/", verbose_name=u"视频资源", max_length=100)
    url = models.CharField(max_length=200, verbose_name="访问地址")

    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 课程资源   课程 1对多 课程资源
class CourseResourse(BaseModel):
    course = models.ForeignKey(Courses, on_delete=models.CASCADE, verbose_name="课程")  # 级联删除
    name = models.CharField(verbose_name="名称", max_length=100)
    # file = models.FileField(verbose_name="下载地址", upload_to="courses/resourse/%Y/%m", max_length=200)
    download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name=u"资源文件", max_length=100)

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseTag(BaseModel):
    course = models.ForeignKey(Courses, on_delete=models.CASCADE, verbose_name="课程")
    tag = models.CharField(max_length=100, verbose_name="标签")

    class Meta:
        verbose_name = "课程标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag


#     课程轮播图
class BannerCourse(Courses):
    class Meta:
        verbose_name = "轮播课程"
        verbose_name_plural = verbose_name
        proxy = True


# 课程讨论
class CourseDiscuss(BaseModel):
    course = models.ForeignKey(Courses, on_delete=models.CASCADE, verbose_name="课程")  # 级联删除
    title = models.CharField(verbose_name="讨论标题", max_length=50)
    discuss_content = models.CharField(verbose_name="讨论内容", max_length=300)
    read_nums = models.IntegerField(verbose_name="阅读数", default=0)

    class Meta:
        verbose_name = "讨论话题"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

    def conment_nums(self):
        return self.coursediscusscomment_set.all().count()
